package com.fingard.dsp.bank.directbank.ccb02;
import java.io.File;
import java.text.MessageFormat;
import java.util.*;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.*;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/9/11.
 */
public class CCB02ElecBill extends CCB02Base implements IElecBillQuery {
    //可操作票据查询
    @Override
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws Exception {
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        query.readRecord();
        retAts.respCode = "0";
        retAts.writeBefore();
        try {
            if ("1".equals(query.detail.queryType)) {
                //查询申请类的票据
                queryBillByApply(query, retAts);
            } else if ("2".equals(query.detail.queryType)) {
                //查询签收类的票据
                queryBillBy6WH028(query, retAts);//待签收票据查询
            } else {
                //查询可撤销的票据
                queryBillByCancel(query, retAts);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    private void queryBillByCancel(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts)throws
    Exception{
        //待撤销票据查询
        if ("1".equals(query.detail.busiType)) {
            queryBillBy6WH027(query, retAts, "0002");
            queryBillBy6WH027(query, retAts, "0003");
        } else {
            switch (Integer.valueOf(query.detail.busiType)) {
                case 2:
                    queryBillBy6WH026(query, retAts, "0001", "020001");
                    break;//承兑撤回
                case 3:
                    queryBillBy6WH026(query, retAts, "0002", "030001");
                    break;//收票撤回
                case 4:
                    queryBillBy6WH026(query, retAts, "0004", "100001");
                    break;//背书撤回
                case 5:
                    queryBillBy6WH026(query, retAts, "0006", "180001");
                    break;//质押撤回
                case 6:
                    queryBillBy6WH026(query, retAts, "0007", "190001");
                    break;//质押解除撤回
                case 10:
                    queryBillBy6WH026(query, retAts, "0005", "110101");
                    queryBillBy6WH026(query, retAts, "0005", "110201");
                    break;//贴现撤回
                case 12:
                    if ("1".equals(query.detail.overdueFlag)) {
                        queryBillBy6WH026(query, retAts, "0008", "200001");
                    } else {
                        queryBillBy6WH026(query, retAts, "0008", "210001");//逾期提示付款待签收
                    }
                    break;//提示付款撤回
                case 13:
                    if ("1".equals(query.detail.recourseType)) {
                        queryBillBy6WH026(query, retAts, "0009", "220707");
                    } else {
                        queryBillBy6WH026(query, retAts, "0009", "220607");//拒付追索待清偿
                    }
                    break;//追索撤回
                case 14:
                    queryBillBy6WH026(query, retAts, "0010", "230601");
                    queryBillBy6WH026(query, retAts, "0010", "230701");
                    break;//非拒付追索同意清偿待签收
                default:
                    throw new Exception("未知的撤回业务类型：" + query.detail.busiType);
            }
        }
    }

    //查询申请类的票据
    private void queryBillByApply(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts)
      throws Exception{
            String BkTranType;
            switch (Integer.valueOf(query.detail.busiType)) {
                case 1:
                    BkTranType = "0000";
                    break;//出票
                case 2:
                    BkTranType = "0001";
                    break;//承兑
                case 3:
                    BkTranType = "0002";
                    break;//提示收票
                case 4:
                    BkTranType = "0004";
                    break;//背书转让
                case 5:
                    BkTranType = "0006";
                    break;//质押
                case 6:
                    BkTranType = "0007";
                    break;//质押解除
                case 10:
                    BkTranType = "0005";
                    break;//贴现
                case 12:
                    BkTranType = "0008";
                    break;//提示付款
                case 13:
                    if ("1".equals(query.detail.recourseType)) {
                        BkTranType = "0009";
                    } else {
                        BkTranType = "0010";
                    }
                    break;//追索
                case 14:
                    BkTranType = "0014";
                    break;//同意清偿
                default:
                    throw new Exception("未知的申请操作类型");
            }
            //出票查询
            if ("0000".equals(BkTranType)) {
                if ("1".equals(query.detail.billType)) {
                    List<String> protocols = getProtocols();
                    for (String protocol : protocols) {
                        queryBillBy6WH024(query, retAts, protocol);
                    }
                }
            } //同意清偿申请
            else if ("0014".equals(BkTranType)) {
                queryBillBy6WH028(query, retAts);
            } else {
                queryBillBy6WH027(query, retAts, BkTranType);//待申请票据查询
            }
    }


    private void queryBillBy6WH024(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts, String protocol)throws Exception{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH024";
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("TX_INFO");
//            BkFlag1 功能号 varChar(1) F 0:查询正常协议 1:查询失效协议
            xmlWriter.writeElementString("BkFlag1","0");
//            BkListNo1 协议编号 varChar(40) F CLPM合同编号
            xmlWriter.writeElementString("BkListNo1",protocol);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length()==0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if(StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)){
                        while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                            String drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                            if (query.detail.actNum.equals(drawAct)) {
                                RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//                             11 BkAcctNo1 出票账号 varChar(32) F 　
                                detail.drawAct = drawAct;
//                            BkListNo1 CLPM票据编号 varChar(10) F 唯一标识一笔出票信息 　
//                                detail.billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                detail.billNo = protocol;
                                detail.busiNo = protocol;
//                                detail.busiNo =protocol+"_"+detail.billNo;
//                            10 Bk60Name1 出票人名称 varChar(120) F 　
                                detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");

//                            12 Bk60Name4 承兑人名称 varChar(120) F 　
                                detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name4");
//                            13 Bk60Name2 收款人名称 varChar(120) F 　
                                detail.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
//                            14 BkAcctNo2 收款人账号 varChar(32) F 　
                                detail.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
//                            15 BkBrchNo2 收款人开户行 varChar(12) F 　
                                detail.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo2");
//                            16 Bk8Date1 出票日期 varChar(8) F 用户输入的日期，预约出票时，用户可以查看并修改此日期
                                detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
//                            17 BkAmt1 出票金额 Decimal(18,2) F 　
                                detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
//                            18 Bk8Date2 汇票到期日 varChar(8) F 　
                                detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
//                            19 Bk8Date3 协议初始出票日期 varChar(8) F 签订协议时，暂定的出票日期
//                            20 BkAcctNo3 保证金账号 varChar(32) F 　
//                            21 BkRate 保证金比例 Decimal(7，5) F 0-1的小数，网银需显示为%格式
//                            25 BkListNo3 交易合同编号 varChar(30) T
                                detail.contractNo = tmpbkReader.readedXElement.getSingleText("BkListNo3");
//                            26 BkListNo4 发票号码 varChar(30) T
                                detail.invoiceNo = tmpbkReader.readedXElement.getSingleText("BkListNo4");
                                detail.billType = "EM001";
                                retAts.writeDetail(detail);
                                break;
                            }else{
                                //同一协议下面的出票人相同
                                break;
                            }
                        }
                    }else{
                        throw new Exception(return_msg);
                    }
                }else{
                    throw new Exception(tmpbkReader.toString());
                }
                 }else {
                    throw new Exception(bankResult[1]);
            }
    }

    private void queryBillBy6WH024(String actNum,RetAtsB009DrawBillQry retAts ,RetAtsB009DrawBillQryProtocolDetail protocolDetail)throws Exception{
        boolean isConfrom = false;
        XmlTextWriter xmlWriter = new XmlTextWriter();
//        XmlTextWriter applyXml = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH024";
        writeCommonHeader(xmlWriter,header);
        xmlWriter.writeStartElement("TX_INFO");
//            BkFlag1 功能号 varChar(1) F 0:查询正常协议 1:查询失效协议
        xmlWriter.writeElementString("BkFlag1","0");
//            BkListNo1 协议编号 varChar(40) F CLPM合同编号
        xmlWriter.writeElementString("BkListNo1",protocolDetail.protocolNo);
        xmlWriter.writeEndDocument();
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if(bankResult[0].length()==0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
            if (tmpbkReader != null) {
                String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                if(StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)){
                    while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                        String drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                        if (actNum.equals(drawAct)) {
//                            if(!isConfrom){
                                //组装申请报文
//                                writeApplyHeader(applyXml,actNum,protocolDetail);
//                            }
                            isConfrom = true;
                            if(StringHelper.isNullOrEmpty(retAts.respCode)){
                                retAts.respCode ="0";
                            }
                            RetAtsB009DrawBillQryTransDetail detail = new RetAtsB009DrawBillQryTransDetail();
//                             BkListNo1 CLPM票据编号 varChar(10) F 唯一标识一笔出票信息 　
                            detail.billNum = tmpbkReader.readedXElement.getSingleText("BkListNo1");
//                            10 Bk60Name1 出票人名称 varChar(120) F 　
                            detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");
//                            11 BkAcctNo1 出票账号 varChar(32) F 　
                            detail.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
//                            12 Bk60Name4 承兑人名称 varChar(120) F 　
                            detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name4");
//                            13 Bk60Name2 收款人名称 varChar(120) F 　
                            detail.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
//                            14 BkAcctNo2 收款人账号 varChar(32) F 　
                            detail.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
//                            15 BkBrchNo2 收款人开户行 varChar(12) F 　
                            detail.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo2");
//                            16 Bk8Date1 出票日期 varChar(8) F 用户输入的日期，预约出票时，用户可以查看并修改此日期
                            detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
//                            17 BkAmt1 出票金额 Decimal(18,2) F 　
                            detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
//                            18 Bk8Date2 汇票到期日 varChar(8) F 　
                            detail.dueDate =DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
//                            20 BkAcctNo3 保证金账号 varChar(32) F 　
                            detail.marginAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo3");
//                            21 BkRate 保证金比例 Decimal(7，5) F 0-1的小数，网银需显示为%格式
                            detail.marginRate = tmpbkReader.readedXElement.getSingleText("BkRate");
//                            23 BkNum3 期限 int F 　
                            detail.billItem = tmpbkReader.readedXElement.getSingleText("BkNum3");
//                            24 BkType1 期限单位 varChar(1) F 0:天 1:月
                            detail.itemUnit = tmpbkReader.readedXElement.getSingleText("BkType1");
//                            25 BkListNo3 交易合同编号 varChar(30) T 　
                            detail.contractNo = tmpbkReader.readedXElement.getSingleText("BkListNo3");
//                            26 BkListNo4 发票号码 varChar(30) T 　
                            detail.invoiceNo = tmpbkReader.readedXElement.getSingleText("BkListNo4");
                            protocolDetail.list.add(detail);
                            //把票据明细加到申请报文中
//                            writeApplyDetail(applyXml,detail);
                        }else{
                            //同一协议下面的出票人相同
                            break;
                        }
                    }
                }else{
                    throw new Exception(return_msg);
                }
            }else{
                throw new Exception(tmpbkReader.toString());
            }
        }else {
            throw new Exception(bankResult[1]);
        }
        if(isConfrom){
            protocolDetail.writeDetail(retAts.retXmlWriter,protocolDetail);
            //添加最终协议信息
//            writeApplyProtocol(applyXml,protocolDetail);
//            applyXml.writeEndDocument();
        }
//        String tmpDateDir = Format.DateForLogFormat.format(new Date()).replace("-", File.separator);
//        String mpSubFileName = FGBiz.dspCenter.bankLogsFirstDir + File.separator + tmpDateDir + File.separator + this.reqHeader.bankCode + File.separator + "B113" + "_协议【" + protocolDetail.protocolNo+"】出票模板_"+Format.DateTime14Format.format(new Date()) + ".txt";
//        String tmpLogFile = FGBiz.dspCenter.bankLogsDirPath + File.separator + mpSubFileName;
//        FileHelper.writeFile(tmpLogFile, applyXml.getSbXml());

    }

    private void writeApplyProtocol(XmlTextWriter applyXml, RetAtsB009DrawBillQryProtocolDetail protocolDetail) {
        applyXml.writeElementString("ProtocolNo",protocolDetail.protocolNo);
        applyXml.writeElementString("TotalAmount",protocolDetail.protocolAmount);
        applyXml.writeElementString("TotalNum",protocolDetail.list.size()+"");
        applyXml.writeElementString("ReqBatchNo",getQryTransID());
        applyXml.writeElementString("CreditLv","");
        applyXml.writeElementString("GradeAgent","");
        applyXml.writeElementString("GradeDueDate","");
        applyXml.writeElementString("ReqReserve1","");
        applyXml.writeElementString("ReqReserve2","");
    }

    private void writeApplyDetail(XmlTextWriter applyXml, RetAtsB009DrawBillQryTransDetail detail)throws Exception {
        applyXml.writeStartElement("DetailRecord");
        applyXml.writeElementString("BillNum",detail.billNum);
        if("EM001".equals(detail.billType)) {
            applyXml.writeElementString("BillType", "1");
        }else if ("EM002".equals(detail.billType)){
            applyXml.writeElementString("BillType", "2");
        }else{
            applyXml.writeElementString("BillType", "");
        }
        applyXml.writeElementString("BillAmt",detail.billAmt);
        applyXml.writeElementString("BillCur","");
        applyXml.writeElementString("IssDate",
                Format.DateTime21Format.format(Format.Date10Format.parse(detail.issDate)));
        applyXml.writeElementString("DueDate",
                Format.DateTime21Format.format(Format.Date10Format.parse(detail.dueDate)));
        applyXml.writeElementString("ContractNo",detail.contractNo);
        applyXml.writeElementString("InvoiceNo",detail.invoiceNo);
        applyXml.writeElementString("TransFlag","1");
        applyXml.writeElementString("MustPay","");
        applyXml.writeElementString("Memo","");
        applyXml.writeElementString("DrawName",detail.drawName);
        applyXml.writeElementString("DrawAct",detail.drawAct);
        applyXml.writeElementString("DrawActArea","");
        applyXml.writeElementString("DrawCode","");
        applyXml.writeElementString("DrawBnkName",detail.drawBnkName);
        applyXml.writeElementString("DrawBnkCode",detail.drawBnkCode);
        applyXml.writeElementString("AccpName",detail.accpName);
        applyXml.writeElementString("AccpAct",detail.accpAct);
        applyXml.writeElementString("AccpCode","");
        applyXml.writeElementString("AccpBnkName",detail.accpBnkName);
        applyXml.writeElementString("AccpBnkCode",detail.accpBnkCode);
        applyXml.writeElementString("AccpSameBnk","");
        applyXml.writeElementString("PayeeName",detail.payeeName);
        applyXml.writeElementString("PayeeAct",detail.payeeAct);
        applyXml.writeElementString("PayeeBnkName",detail.payeeBnkName);
        applyXml.writeElementString("PayeeBnkCode",detail.payeeBnkCode);
        applyXml.writeElementString("PayeeSameBnk","");
        applyXml.writeElementString("PostScript","");
        applyXml.writeElementString("ReqReserve3","");
        applyXml.writeElementString("ReqReserve4","");
        applyXml.writeEndElement();
    }

    private void writeApplyHeader(XmlTextWriter applyXml, String actNum, RetAtsB009DrawBillQryProtocolDetail protocolDetail) {
        applyXml.writeStartDocument();
        applyXml.writeStartElement("ATSYH");
        applyXml.writeStartElement("TransReq");

        applyXml.writeElementString("BankCode", this.reqHeader.bankCode);
        applyXml.writeElementString("Entity", this.reqHeader.corporation);
        applyXml.writeElementString("SystemCode", "");
        applyXml.writeElementString("TransType", "B113");
        applyXml.writeElementString("TransSeqID", getQryTransID());
        applyXml.writeElementString("TransTime", Format.DateTimeFormat.format(new Date()));
        applyXml.writeStartElement("TransParam");
    }


    public List<RetAtsB009DrawBillQryProtocolDetail> getProtocolDetails(){
        List<RetAtsB009DrawBillQryProtocolDetail> protocols = new ArrayList<RetAtsB009DrawBillQryProtocolDetail>();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH023";
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("TX_INFO");
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length()==0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if(StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)){
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                                if("1".equals(tmpbkReader.readedXElement.getSingleText("BkFlag3"))){
                                    RetAtsB009DrawBillQryProtocolDetail detail = new RetAtsB009DrawBillQryProtocolDetail();
//                                    10 BkListNo1 协议编号 varChar(40) F CLPM合同编号
                                    detail.protocolNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
//                                    11 Bk8Date1 协议签订日期 varChar(8) F 合同起始日
                                    detail.startDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
//                                    12 BkAmt1 协议额度 Decimal(18.2) F 　
                                    detail.protocolAmount = tmpbkReader.readedXElement.getSingleText("BkAmt1");
//                                    13 BkNum2 期限 int F 　
                                    detail.protocolItem = tmpbkReader.readedXElement.getSingleText("BkNum2");
//                                    14 BkType4 期限单位 char(1) F 0:天 1:月
                                    detail.itemUnit = tmpbkReader.readedXElement.getSingleText("BkType4");
//                                    15 BkFlag3 协议状态 char(1) F 0：失效 1：正常
                                    detail.protocolState = tmpbkReader.readedXElement.getSingleText("BkFlag3");
                                    protocols.add(detail);
                                }
                            }
                        }else{
                           throw new Exception(return_msg);
                    }
                }else{
                    throw new Exception(tmpbkReader.toString());
                }
            }else {
                throw new Exception(bankResult[1]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return protocols;
    }

    public List<String> getProtocols(){
        List<String> protocols = new ArrayList<String>();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH023";
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("TX_INFO");
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length()==0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if(StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)){
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                                if("1".equals(tmpbkReader.readedXElement.getSingleText("BkFlag3"))){
                                    String protocol = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                    if(StringHelper.hasAnyChar(protocol)){
                                        protocols.add(protocol);
                                    }
                                }
                            }
                        }else{
                           throw new Exception(return_msg);
                    }
                }else{
                    throw new Exception(tmpbkReader.toString());
                }
            }else {
                throw new Exception(bankResult[1]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return protocols;
    }

    private void queryBillBy6WH026(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts,String transtype, String billStatus) throws Exception {
        int monthLength = getBankConfig().getValueAsInt("DateBeforeNow");
        if(monthLength<=0){
            monthLength=3;
        }
        Calendar tmpCalendarTo = Calendar.getInstance();
        Calendar tmpCalendarFrom = Calendar.getInstance();
        tmpCalendarFrom.add(Calendar.MONTH,0-monthLength);
        int tmpIntDateFrom = 0;
        int tmpIntDateTo = 0;

        while (tmpIntDateFrom <= tmpIntDateTo) {
            Calendar tmpClndrStepTo = Calendar.getInstance();
            tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
            //判断是否跨月
            if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                //跨月则设置为月末
                tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
            } else {
                tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
            }
            int startPage = 0;
            int totalNum = 0;
            int tmpReadNum = 0;
            do {
                startPage++;
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.TX_CODE = "6WH026";
                header.REQUEST_SN = getQryTransID();
                writeCommonHeader(xmlWriter, header);
                xmlWriter.writeStartElement("TX_INFO");
//            BKTYPE 票据类型 NUMBER(4) F AC01银承 ;AC02商承
                xmlWriter.writeElementString("BKTYPE", "1".equals(query.detail.billType) ? "AC01" : "AC02");
//            9 BkType1 票据状态 CHAR(6) T 客户要做撤回的时候，会送待签收的票据状态过来；但由于贴现、追索、清偿等分为两种情况，而且客户做撤回需要将这两种情况的都显示，所以需要规定特殊的状态（其他情况，一律为实际的票据状态）
                xmlWriter.writeElementString("BkType1", billStatus);
//            10 Bk10Date1 查询起始日期 varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk10Date1", Format.Date8Format.format(tmpCalendarFrom.getTime()));
//            11 Bk10Date2 查询结束日期 varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk10Date2", Format.Date8Format.format(tmpClndrStepTo.getTime()));
//            12 Bk8Date3 票据到期日开始  varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk8Date3", "");
//            13 Bk8Date4 票据到期日结束  varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk8Date4", "");
//            14 Bk60Name1 收款人名称 varChar(120) T 　
                xmlWriter.writeElementString("Bk60Name1", "");
//            15 BkAmt1 起始金额 NUMBER(18.2) T 　
                xmlWriter.writeElementString("BkAmt1", "");
//            16 BkAmt2 结束金额 NUMBER(18.2) T 　
                xmlWriter.writeElementString("BkAmt2", "");
//            17 BkListNo1 电子票据号码 varChar(30) T 　
                xmlWriter.writeElementString("BkListNo1", "");
//            18 BkType2 交易类型 varChar(4) F
                xmlWriter.writeElementString("BkType2", transtype);
//            19 PAGE 当页号 NUMBER(20) F 默认为1
                xmlWriter.writeElementString("PAGE", startPage + "");
//            20 PAGE_SIZE 每页显示的记录数 NUMBER(20) F 默认为200条记录数
                xmlWriter.writeElementString("PAGE_SIZE", "200");
                xmlWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlWriter.toXmlString());
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpbkReader != null) {
                        String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                        if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {

                            if (0 == totalNum && tmpReadNum == 0) {
                                totalNum = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum"));//总数
                            }
                            //记录查询返回总数
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                tmpReadNum++;
                                String applyNum = tmpbkReader.readedXElement.getSingleText("BkAcctNo7");
                                if (query.detail.actNum.equals(applyNum)) {
                                    RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                    detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
                                    detail.billType = "1".equals(query.detail.billType) ? "EM001" : "EM002";
                                    detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
                                    detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
                                    detail.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                                    detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");
                                    detail.drawBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo1");
                                    detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name4");
                                    detail.accpBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo4");
                                    detail.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
                                    detail.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
                                    detail.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo2");
                                    detail.billStatus = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                                    detail.busiNo = tmpbkReader.readedXElement.getSingleText("BkOldPlatDate") + "_" +
                                            tmpbkReader.readedXElement.getSingleText("BkOldSeq");
                                    retAts.writeDetail(detail);
                                }
                            }
                        } else {
                            throw new Exception(return_msg);
                        }
                    } else {
                        throw new Exception(tmpSbMsg.toString());
                    }
                } else {
                    throw new Exception(bankResult[1]);
                }
            } while (tmpReadNum < totalNum);
            tmpCalendarFrom = tmpClndrStepTo;
            tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
            tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
            tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
        }
    }

    private void queryBillBy6WH028(ReqB001OperBillQry query,RetAtsB001OperBillQry retAts)throws Exception {
            int tmpReadNum = 0;//已查询笔数
            int tmpTotalNum = 0;

           do{
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN = getQryTransID();
                header.TX_CODE = "6WH028";
                writeCommonHeader(xmlTextWriter, header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                    7 ECTYPEVALUE          电票类型              varChar(4) F AC01银承,AC02商承
                String billType;
                if ("1".equals(query.detail.billType)) {
                    billType = "AC01";
                } else {
                    billType = "AC02";
                }
//                8 BkType1 票据类型 Char(4) F AC01银承
//                AC02商承
                xmlTextWriter.writeElementString("BkType1",billType);
//                9 Bk8Date1 交易日期 Char(8) T 　
                xmlTextWriter.writeElementString("Bk8Date1","");
//                10 BkType2 应答类型 Char(4) F 0001其他
//                0002同意清偿
//                0003.交票待签收应答 0004.背书待签收应答
                String busiType;
               if("2".equals(query.detail.queryType)) {
                   switch (Integer.valueOf(query.detail.busiType)) {
                       case 2:
                       case 5:
                       case 6:
                       case 11:
                       case 12:
                       case 14:
                           busiType = "0001";
                           break;//其他
                       case 3:
                           busiType = "0003";
                           break;//收票待签收
                       case 4:
                           busiType = "0004";
                           break;//背书待签收
                       default:
                           throw new Exception("业务类型未知：" + query.detail.busiType);
                   }
               }else{
                   busiType = "0002";
               }
                xmlTextWriter.writeElementString("BkType2",busiType);
//                11 __BEGIN_REC 开始记录数 Int F 　
                xmlTextWriter.writeElementString("__BEGIN_REC",tmpReadNum+1+"");
//                12 __MAX_REC 一次查询最大记录数 Int F
                xmlTextWriter.writeElementString("__MAX_REC","200");
                xmlTextWriter.writeEndDocument();
                String[] result = sendToBank(xmlTextWriter.toXmlString());
                if (result[0].length() == 0) {
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                    if (tmpbkReader != null) {
                        String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {
                            String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
                            //记录查询返回总数
                            if (tmpReadNum == 0 && tmpTotalNum == 0) {
                                tmpTotalNum = Integer.valueOf(totalNum);
                            }
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                tmpReadNum++;
                                //38 BkAcctNo7 签收人账号 varChar(32) F
                                String signAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo7");
                                if (!signAct.equals(query.detail.actNum)) {
                                    continue;
                                }
//                                BkType2 应答类型 Char(4) F
//                                0:收款人应答
//                                1:出票保证人应答
//                                2:承兑保证人应答
//                                3:背书保证人应答（1，2，3目前根据人行接口无法区分具体何种保证，返回网银统一为1）
//                                4:承兑应答
//                                5:背书转让应答
//                                6:付款应答
//                                7：追索应答（被追索人收到追索人发起的追索申请后，可以发起同意清偿或不予处理）
//                                8:同意清偿应答（追索人收到被追索人发起的清偿申请后，做签收或驳回）
//                                9.质押应答（只有商承）
//                                10:质押解除应答
//                                11:贴现赎回应答
                                String bktype2 = tmpbkReader.readedXElement.getSingleText("BkType2");
                                if (!checkBkType(Integer.parseInt(query.detail.busiType),query.detail.queryType, bktype2, query.detail.billType)) {
                                    continue;
                                }
                                RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//                                13 BkListNo1 电子票据号码 varChar(30) F 　
                                detail.billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                //票据类型
                                detail.billType = "1".equals(query.detail.billType) ? "EM001" : "EM002";
                                //27 Bk8Date1 出票日期 Char(8) F 　
                                detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
                                //29 Bk8Date2 汇票到期日 Char(8) F 　
                                detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
//                                26 BkAmt1 出票金额 Decimal(18,2) F 　
                                detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
//                                28 BkType3 不得转让标识 Char(4) F 票据的属性
                                String transFlag = tmpbkReader.readedXElement.getSingleText("BkType3");
                                if ("EM00".equals(transFlag)) {
                                    detail.transFlag = "1";
                                } else {
                                    detail.transFlag = "0";
                                }
                                //承兑无条件支付委托 1-无条件支付 0-非条件支付
                                detail.mustPay = "";
                                //14 Bk60Name1 出票人名称 varChar(120) F 　
                                detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");
                                //16 BkBrchNo1 出票人开户行 varChar(12) F 　
                                detail.drawBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo1");
                                //15 BkAcctNo1 出票人账号 varChar(32) F 　
                                detail.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                                //20 Bk60Name3 承兑人名称 varChar(120) F 　
                                detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name3");
                                //21 BkBrchNo3 承兑人开户行 varChar(12) F 　
                                detail.accpBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo3");
                                //17 Bk60Name2 收款人名称 varChar(120) F 　
                                detail.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
                                //18 BkAcctNo2 收款人账号 varChar(32) F 　
                                detail.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
                                //19 BkBrchNo2 收款人开户行 varChar(12) F 　
                                detail.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo2");
                                //交易明细流水：原ECDS平台日期+原ECDS平台流水
                                detail.busiNo = tmpbkReader.readedXElement.getSingleText("BkOldPlatDate") + "_" + tmpbkReader.readedXElement.getSingleText("BkOldSeq");
                                //33 Bk60Name6 申请人名称 varChar(120) T 　
                                detail.applyName = tmpbkReader.readedXElement.getSingleText("Bk60Name6");
                                //34 BkAcctNo6 申请人账号 varChar(32) T 　
                                detail.applyAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo6");
                                //35 BkBrchNo6 申请人开户行行号 varChar(12) T 　
                                detail.applyBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo6");
                                if("0003".equals(busiType)){
                                    if(StringHelper.isNullOrEmpty(detail.applyAct)){
                                        detail.applyAct = detail.drawAct;
                                    }
                                    if(StringHelper.isNullOrEmpty(detail.applyName)){
                                        detail.applyName = detail.drawName;
                                    }
                                    if(StringHelper.isNullOrEmpty(detail.applyBnkCode)){
                                        detail.applyBnkCode = detail.drawBnkCode;
                                    }
                                }
//                                Bk60Name7 签收人名称 varChar(120) F 当应答类型为8时返回被追索人
                                detail.receName = tmpbkReader.readedXElement.getSingleText("Bk60Name7");
//                                38 BkAcctNo7 签收人账号 varChar(32) F
                                detail.receAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo7");
//                                39 BkBrchNo7 签收人开户行行号 varChar(12) F
                                detail.receBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo7");
                                //36 BkDetail1 申请人备注 varChar(512) T 　
                                detail.memo = tmpbkReader.readedXElement.getSingleText("BkDetail1");
                                //逾期原因说明
                                detail.overDueRea = tmpbkReader.readedXElement.getSingleText("Bk60Name8");
                                if("6".equals(bktype2)){
                                    if(StringHelper.hasAnyChar(detail.overDueRea)) {
                                        detail.overDueFlag = "2";
                                    }else{
                                        detail.overDueFlag = "1";
                                    }
                                }
                                //追索类型 1-拒付追索 2-非拒付追索
                                detail.recourseType = "";
                                //追索金额
                                String recoAmt = tmpbkReader.readedXElement.getSingleText("BkAmt2");
                                if (StringHelper.hasAnyChar(recoAmt)) {
                                    detail.recourseAmt = recoAmt;
                                }
                                //30 BkFlag1 票据状态 Char(6) F 　
                                detail.billStatus = tmpbkReader.readedXElement.getSingleText("BkFlag1");
//                                31 BkOldPlatDate 原ECDS平台日期 Char(8) F 　
//                                32 BkOldSeq 原ECDS平台流水号 Int F 　
//                                40 BkAmt2 清偿金额 Decimal(18,2) T 在应答类型为7,8时，必须返回
                                detail.recourseAmt = tmpbkReader.readedXElement.getSingleText("BkAmt2");

                                retAts.writeDetail(detail);
                            }
                            //状态码为YBLECDA10000，表示查询明细不存在
                        }else if("YBLECDA10000".equals(return_code)||"YBLECDA66261".equals(return_code)) {
                                break;
                        }else {
                            throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                        }
                    } else {
                        throw new Exception(tmpbkReader.toString());
                    }
                } else {

                    throw new Exception(result[1]);

                }
            }while (tmpReadNum<tmpTotalNum);
    }

    private void queryBillBy6WH027(ReqB001OperBillQry query,RetAtsB001OperBillQry retAts,String tranType)throws Exception {
        //银承不支持承兑同步
        if("1".equals(query.detail.billType)&&"0001".equals(tranType)){
            return ;
        }
            int page = 0;//起始页数
            int tmpReadNum = 0;//已查询笔数
            int tmpTotalNum = 0;//记录总数
            do{
                page++;
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN = getQryTransID();
                header.TX_CODE = "6WH027";
                writeCommonHeader(xmlTextWriter, header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                    7 ECTYPEVALUE          电票类型              varChar(4) F AC01银承,AC02商承
                String billType;
                if ("1".equals(query.detail.billType)) {
                    billType = "AC01";
                } else {
                    billType = "AC02";
                }
                xmlTextWriter.writeElementString("ECTYPEVALUE", billType);
//              8 BkTranType 交易类型 varChar(4) F 0001：承兑（在票据类型为：AC02时候，客户可以发起查询）
//              0002：交票/未用退回 0003：退票（只查询出票已经登记的票据）0004：背书 0005：贴现 0006：质押 0007：质押解除
//              0008：提示付款 0009：拒付追索 0010：非拒付追索 0011：追索（包括拒付和非拒付）0012：查验持票查询

                xmlTextWriter.writeElementString("BkTranType", tranType);
//                    9 ECDSNO               汇票号码             varChar(30) T 　
                xmlTextWriter.writeElementString("ECDSNO", "");
//                    10 BEGINDATE            票据到期日开始         varChar(8) T YYYYMMDD
                xmlTextWriter.writeElementString("BEGINDATE", "");
//                    11 ENDDATE              票据到期日结束         varChar(8) T YYYYMMDD
                xmlTextWriter.writeElementString("ENDDATE", "");
//                    12 BRCHNO               承兑银行名称 varchar(120) T 　
                xmlTextWriter.writeElementString("BRCHNO", "");
//                    13 BkAmt1 票据金额开始 Number(18,2) T 0或空表示最大值.
                xmlTextWriter.writeElementString("BkAmt1", "");
//                    14 BkAmt2 票据金额结束 Number(18,2) T
                xmlTextWriter.writeElementString("BkAmt2", "");
//                    15 Bk60Name1            出票人名称           varchar(120) T 　
                xmlTextWriter.writeElementString("Bk60Name1", "");
//                    16 Bk8Date3             票据出票日开始       varChar(8) T YYYYMMDD
                xmlTextWriter.writeElementString("Bk8Date3", "");
//                    17 Bk8Date4             票据出票日结束       varChar(8) T YYYYMMDD
                xmlTextWriter.writeElementString("Bk8Date4", "");
//                    18 PAGE                 当前页次             Number(20) T 默认为1
                xmlTextWriter.writeElementString("PAGE", page + "");
//                    19 PAGE_SIZE            每页记录数           Number(20) T 默认为200条记录数
                xmlTextWriter.writeElementString("PAGE_SIZE", "200");
//                    20 BkType4            集团划转标志           Char(1) T 区分上手是否为集团本部标志:0001待转集团 0002 集团划转只有是集团票据才上送以上两个标志
                xmlTextWriter.writeElementString("BkType4", "");
//                    21 BkOthName            收款人名称           varchar(120) T
                xmlTextWriter.writeElementString("BkOthName", "");
                xmlTextWriter.writeEndDocument();
                String[] result = sendToBank(xmlTextWriter.toXmlString());
                if (result[0].length() == 0) {
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                    if (tmpbkReader != null) {
                        String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        if ("000000".equals(return_code)) {
                            String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
                            //记录查询返回总数
                            if (tmpReadNum==0&&tmpTotalNum==0) {
                                tmpTotalNum = Integer.valueOf(totalNum);
                                if ("0".equals(totalNum)) {
                                    break;
                                }
                            }
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                tmpReadNum++;
                                String ownAcc = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                                if(query.detail.actNum.equals(ownAcc)) {
                                    RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//                                    <BkListNo2>协议编号</BkListNo2>
//                                    <BkListNo1>电子票据号码</BkListNo1>
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                    billType = tmpbkReader.readedXElement.getSingleText("BkType1");
                                    if ("AC01".equals(billType)) {
                                        detail.billType = "EM001";
                                    } else if ("AC02".equals(billType)) {
                                        detail.billType = "EM002";
                                    }
                                    //<Bk8Date1>出票日期</Bk8Date1>
                                    detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
                                    //<Bk8Date2>汇票到期日</Bk8Date2>
                                    detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
                                    //<BkAmt1>出票金额</BkAmt1>
                                    detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
                                    //转让标识
                                    detail.transFlag = "1";
                                    //承兑无条件支付委托 1-无条件支付 0-非条件支付
                                    detail.mustPay = "";
                                    //出票人
                                    detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
                                    detail.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");

                                    //承兑人开户行
                                    detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name7");
                                    detail.accpAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo7");
                                    detail.accpBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo7");
                                    //<BkFlag1>票据状态</BkFlag1>
                                    detail.billStatus = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                                    //<BkDetail3>备注2</BkDetail3>
                                    detail.memo = tmpbkReader.readedXElement.getSingleText("BkDetail3");
                                    retAts.writeDetail(detail);
                                }
                            }
                        } else {
                            throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                        }
                    } else {
                        throw new Exception(tmpbkReader.toString());

                    }
                } else {
                    throw new Exception(result[1]);
                }
                // TODO: 2019/4/28 建行翻页有问题，先不翻页
            }while (tmpReadNum<tmpTotalNum);
    }

    //票据操作明细查询
    @Override
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws Exception {
        return null;
    }
    //票据正面信息查询 6WH049  已测
    @Override
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query) throws Exception {
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        query.readRecord();
        try {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN = getQryTransID();
                header.TX_CODE = "6WH049";
                writeCommonHeader(xmlTextWriter, header);
    //                <body>
                xmlTextWriter.writeStartElement("TX_INFO");
                xmlTextWriter.writeStartElement("DETAILLIST");
                xmlTextWriter.writeStartElement("DETAILINFO");
                xmlTextWriter.writeElementString("BkListNo10", query.detail.billNo);
                xmlTextWriter.writeEndDocument();
                String[] result = sendToBank(xmlTextWriter.toXmlString());
                if (result[0].length() == 0) {
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                    if (tmpbkReader != null) {
                        String returnCode = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        if ("000000".equals(returnCode)) {
                            if (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                retAts.respCode ="0";
                                retAts.respInfo ="";
                                retAts.billNo=query.detail.billNo;//电子票据号码
                                String billType = tmpbkReader.readedXElement.getSingleText("BkType1");
                                if ("AC01".equals(billType)) {
                                    retAts.billType = "1";
                                } else if ("AC02".equals(billType)) {
                                    retAts.billType = "2";
                                }
                                //<Bk8Date1>出票日期<Bk8Date1>
                                retAts.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
                                //<Bk8Date2>到期日<Bk8Date2>
                                retAts.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
                                //<BkAmt1>票据金额<BkAmt1>
                                retAts.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
    //                                <BkFlag2>转让标记<BkFlag2>
                                String transFlag = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                if ("EM00".equals(transFlag)) {
                                    retAts.transFlag = "1";
                                } else if ("EM01".equals(transFlag)) {
                                    retAts.transFlag = "0";
                                }
                                //出票人
                                retAts.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");
                                retAts.drawCode = tmpbkReader.readedXElement.getSingleText("BkInstNo");
                                retAts.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
                                retAts.drawBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo1");

                                retAts.creditLv = tmpbkReader.readedXElement.getSingleText("BkLevel1");
                                retAts.gradeAgent = tmpbkReader.readedXElement.getSingleText("Bk60Name8");
                                retAts.gradeDueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date3"));
                                retAts.drGuarName = tmpbkReader.readedXElement.getSingleText("BkDetail5");//出票保证人名称
                                retAts.drGuarAddr = tmpbkReader.readedXElement.getSingleText("BkDetail6");//出票保证人地址
                                retAts.drGuarDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("BkElctSgn1"));//出票保证日期
                                //承兑人
                                retAts.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name5");
                                retAts.accpAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo5");
                                retAts.accpBnkName = tmpbkReader.readedXElement.getSingleText("Bk60Name6");
                                retAts.accpBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo4");
                                retAts.apGuarName = tmpbkReader.readedXElement.getSingleText("BkLoginTxCode");//承兑保证人名称
                                retAts.apGuarAddr = tmpbkReader.readedXElement.getSingleText("BkLoginSvc");//承兑保证人地址
                                retAts.apGuarDate = tmpbkReader.readedXElement.getSingleText("BkElctSgn2");//承兑保证日期
                                //收款人
                                retAts.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
                                retAts.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
                                retAts.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo3");
                                //票据状态
                                retAts.billState = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                            }else{
                                throw new Exception("返回报文中没有票据明细标签！");
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpsbMsg.toString();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = result[1];
                }
        } catch (Exception e){
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail();
        retAts.writeEnd();
        return retAts;
    }
    //票据背面查询
    @Override
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception {
        RetAtsB005BillBackQry retAts = new RetAtsB005BillBackQry(query.reqHeader);
        retAts.writeStart();
        retAts.writeRespCode("0");
        retAts.writeRespInfo("");
        query.readRecord();
            //请求报文
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH049";
            writeCommonHeader(xmlTextWriter, header);
            //                <body>
            xmlTextWriter.writeStartElement("TX_INFO");
            xmlTextWriter.writeStartElement("DETAILLIST");
            xmlTextWriter.writeStartElement("DETAILINFO");
            xmlTextWriter.writeElementString("BkListNo10", query.detail.billNo);
            xmlTextWriter.writeEndDocument();
            //请求银行
            String[] result = sendToBank(xmlTextWriter.toXmlString());
            if (result[0].length() == 0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    if ("000000".equals(returnCode)) {
                        String bknum = tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1");
                        if (!"1".equals(bknum)) {

                            throw new Exception("获取票据的背面信息失败");
                        }
                        while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                            String resultArr = tmpbkReader.readedXElement.getSingleText("BkDetail7");
                            //交易类型|申请人名称|被申请人名称|不得转让标识|申请日期||@|$
                            String[] content = resultArr.split("@\\|\\$");
                            for (int i = 0; i < content.length; i++) {
                                String transFlag = "";
                                RetAtsB005BillBackQryDetail detail = new RetAtsB005BillBackQryDetail();
                                String[] rspArray = content[i].split("\\|", -1);
                                detail.respCode = "0";
                                detail.respInfo = "";
                                //电子票据号码
                                detail.billNo = query.detail.billNo;
                                //操作代码取值
                                /// 交易类型代码 2  承兑 3  收票 4  撤票 10  背书转让 11  贴现 12  回购式贴现赎回 13  转贴现 14  回购式转贴现赎回 15  再贴现 16  回购式再贴现赎回 25  央行卖出商业汇票 17  保证 18  质押 19  质押解除 20  提示付款 21  逾期提示付款 22  追索 23  追索同意清偿
                                detail.transCode = getTransCode(rspArray[0].trim());
//                                    00转让背书
//                                    03买断式贴现背书
//                                    05回购式贴现赎回背书
//                                    06买断式转贴现背书
//                                    08回购式转贴现赎回背书
//                                    09买断式再贴现背书
//                                    11回购式再贴现赎回背书
//                                    12央行卖出商业汇票
//                                    类型|背书人名称|被背书人名称|不得转让标记|背书日期| @|$
                                if ("00".equals(rspArray[0].trim()) || "03".equals(rspArray[0].trim()) || "05".equals(rspArray[0].trim())
                                        || "06".equals(rspArray[0].trim()) || "08".equals(rspArray[0].trim()) || "09".equals(rspArray[0].trim())
                                        || "11".equals(rspArray[0].trim()) || "12".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
                                    detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[4].trim());
                                    if ("EM00".equals(rspArray[3].trim())) {
                                        transFlag = "1";
                                    } else if ("EM01".equals(rspArray[3].trim())) {
                                        transFlag = "0";
                                    }
                                    detail.transFlag = transFlag;
                                }
//                                    01保证
//                                    类型|被保证人名称|保证人名称|保证人地址|保证日期| @|$
                                else if ("01".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
                                    detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[4].trim());
                                    //地址
                                    detail.guarAddr = rspArray[3].trim();
                                }
//                                    04回购式贴现背书
//                                    07回购式转贴现背书
//                                    10回购式再贴现背书
//                                    票据类型|背书人名称|被背书人名称|背书日期|赎回开放日|赎回截止日| @|$
                                else if ("04".equals(rspArray[0].trim()) || "07".equals(rspArray[0].trim()) || "10".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
                                    detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[3].trim());
                                    //赎回开放日
                                    detail.repOpenDate = DateHelper.convertD8toD10(rspArray[4].trim());
                                    //赎回截止日
                                    detail.repDueDate = DateHelper.convertD8toD10(rspArray[5].trim());
                                }
//                                    14追索清偿
//                                    15再追索清偿
//                                    类型|追索人名称|清偿人名称|追索日期|追索类型|清偿日期| @|$
                                else if ("14".equals(rspArray[0].trim()) || "15".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
                                    detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[3].trim());
                                    detail.respDate = DateHelper.convertD8toD10(rspArray[5].trim());
                                    //追索类型
                                    if ("RT00".equals(rspArray[4].trim())) {
                                        detail.recourseType = "1";
                                    } else if ("RT01".equals(rspArray[4].trim())) {
                                        detail.recourseType = "2";
                                    }
                                }
//                                    02质押背书
//                                    类型|出质人名称|质权人名称|出质日期|质押解除日期| @|$
                                else if ("02".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
                                    detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[3].trim());
                                }
//                                    13提示付款
//                                    类型|提示付款人名称|提示付款日期|付款或拒付|付款或拒付日期|拒付理由| @|$
                                else if ("13".equals(rspArray[0].trim())) {
                                    //申请人
                                    detail.applyName = rspArray[1].trim();
                                    //接收人
//                                        detail.receName = rspArray[2].trim();
                                    //申请日期
                                    detail.applyDate = DateHelper.convertD8toD10(rspArray[2].trim());
                                    if ("EM00".equals(rspArray[3].trim())) {
                                        detail.respFlag = "1";
                                    } else if ("EM01".equals(rspArray[3].trim())) {
                                        continue;
                                    }
                                    detail.respDate = DateHelper.convertD8toD10(rspArray[5].trim());
                                }
                                retAts.writeDetail(detail);

                            }
                        }
                    } else {
                        throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                    }
                } else {
                    throw new Exception(tmpsbMsg.toString());
                }
            } else {
                throw new Exception( result[1]);
            }
        }catch (Exception ex){
            retAts = new RetAtsB005BillBackQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    //交易结果查询
    @Override
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query) throws Exception {
        RetAtsB006BillStateQry retAts = new RetAtsB006BillStateQry(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();
        try{
            if("1".equals(query.detail.operType)){
                queryStateSubmit(detail,query);
            }else if("4".equals(query.detail.operType)){
                queryStateCancle(detail,query);
            }else{
                queryStateResp(detail,query);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            WriteBankLogLn("交易结果查询异常:"+ex.getMessage());
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 对应答类的操作做状态查询 (6WH028）应答查询
     * @param detail
     * @param query
     */
    private void queryStateResp(RetAtsB006BillStateQryDetail detail, ReqB006BillStateQry query) throws Exception {

        //int start =1;
        //int total = 0;
        //判断只有一笔符合条件的记录
        int tmpNowReadStart = 1;//起始页数
        int tmpNowReadNum = 0;//已查询笔数
        boolean flag = false;
        Map<String, Integer> tmpTotalNum = new HashMap<String, Integer>();
        while (tmpNowReadNum == 0 || tmpNowReadNum < tmpTotalNum.get("recordCount")) {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH041";
            writeCommonHeader(xmlWriter, header);
//        Transaction_Body
            xmlWriter.writeStartElement("TX_INFO");
//            BkType1 票据类型 Char(4) F AC01银承 AC02商承
            String billType = "";
            if ("1".equals(query.detail.billType)) {
                billType = "AC01";
            } else if ("2".equals(query.detail.billType)) {
                billType = "AC02";
            }
            xmlWriter.writeElementString("ECTYPEVALUE", billType);
//            9 Bk8Date1 交易日期 Char(8) T 　
            //String operDate = Format.DateTime21Format.format(query.detail.beginDate);
            String operDate = "";
            //1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
            //0001：承兑回复0002：出票回复0003：背书回复0004：贴现回复0005：贴现赎回回复0006：转贴现回复0007：转贴现赎回回复0009：再贴现赎回回复0010：央行卖票回复0011：保证回复0012：质押回复0013：质押解除回复0014：提示付款回复0015：逾期提示付款回复0016：同意清偿回复
            String busiType = "";
            switch (Integer.valueOf(query.detail.busiType)) {
                case 2:busiType = "0001";break;
                case 3:busiType = "0002";break;
                case 4:busiType = "0003";break;
                case 5:busiType = "0012";break;
                case 6:busiType = "0013";break;
                case 7:busiType = "0011";break;
                case 8:busiType = "0011";break;
                case 9:busiType = "0011";break;
                case 10:busiType = "0004";break;
                case 11:busiType = "0005";break;
                case 12:
                    if ("1".equals(query.detail.overdueFlag)) {
                        busiType = "0014";//正常提示付款
                    } else if ("2".equals(query.detail.overdueFlag)) {
                        busiType = "0015";//逾期提示付款
                    }
                    break;
                case 13:throw new Exception("暂不支持追索回复");
                case 14:busiType = "0016";break;
                default:throw new Exception("类型异常："+query.detail.busiType);
            }
            xmlWriter.writeElementString("TRANTYPE", busiType);

            String begindate = query.detail.operateDate.substring(0,10).replace("-","");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(Format.Date8Format.parse(begindate));
            calendar.add(Calendar.DAY_OF_MONTH,20);
            String enddate = Format.Date8Format.format(calendar.getTime());
            xmlWriter.writeElementString("BEGINDATE", begindate);
            xmlWriter.writeElementString("ENDDATE", enddate);
            String replayType = "";
            if ("2".equals(query.detail.operType)) {
                replayType = "SU00";
            } else if ("3".equals(query.detail.operType)) {
                replayType = "SU01";
            }
            xmlWriter.writeElementString("RESPONSE", replayType);//应答结果
            //7-出票保证 8-承兑保证 9-背书保证
            String ensureType = "";
            if ("7".equals(query.detail.busiType)) {
                ensureType = "0";
            } else if ("8".equals(query.detail.busiType)) {
                ensureType = "1";
            } else if ("9".equals(query.detail.busiType)) {
                ensureType = "2";
            }
            xmlWriter.writeElementString("BkFlag3", ensureType);//保证类型
//        19 PAGE 当页号 NUMBER(20) F 默认为1
            xmlWriter.writeElementString("PAGE", tmpNowReadStart + "");
//        20 PAGE_SIZE 每页显示的记录数 NUMBER(20) F 默认为200条记录数
            xmlWriter.writeElementString("PAGE_SIZE", "200");
            xmlWriter.writeEndDocument();
            String[] result = sendToBank(xmlWriter.toXmlString());
            if (result[0].length() == 0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                if (tmpbkReader != null) {
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {
                        String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
                        String returnNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1");//返回数
                        //
                        if(Integer.parseInt(totalNum)>0 && Integer.parseInt(returnNum)>0){
                            //记录总条数，分页使用
                            tmpTotalNum.put("recordCount", Integer.valueOf(totalNum));
                            //遍历返回数据，匹配对应票据操作的状态
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                String memo = tmpbkReader.readedXElement.getSingleText("BkDetail2");
                                String billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                if (StringHelper.hasAnyChar(memo)&&memo.contains(query.detail.reqSeqID) && query.detail.billNo.equals(billNo)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpbkReader.readedXElement.getSingleText("BkSumry");
                                    detail.billState = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                                    detail.transState = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                    detail.payInfoCode = detail.transState;
                                    flag = true;
                                    break;
                                }
                            }
                            if(flag){
                                break;
                            }
                            //校验是否需要查询下一页
                            tmpNowReadNum += Integer.parseInt(returnNum);//已查询数
                            if (tmpTotalNum.get("recordCount") > tmpNowReadNum) {
                                tmpNowReadStart++;
                            }
                        }else {
                            throw new Exception("没找到符合的记录");
                        }
                    } else {
                        throw new Exception(return_msg);
                    }
                } else {
                    throw new Exception(tmpsbMsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        }
        if(!flag){
            throw new Exception("没找到符合的记录");
        }
    }

    /**
     * 对申请类的操作做状态查询 6WH026）外联平台结果查询
     * @param detail
     * @param query
     */
    private void queryStateSubmit(RetAtsB006BillStateQryDetail detail, ReqB006BillStateQry query) throws Exception{
        int page =0;
        int total = 0;
        int tmpReadNum =0;
        //判断只有一笔符合条件的记录
        boolean flag = false;
        do{
            page++;
        XmlTextWriter xmlWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN =getQryTransID();
        header.TX_CODE = "6WH026";
        writeCommonHeader(xmlWriter,header);
//        Transaction_Body
        xmlWriter.writeStartElement("TX_INFO");
//        8 BKTYPE 票据类型 NUMBER(4) F AC01银承 ;AC02商承
        String billType ="";
        if("1".equals(query.detail.billType)){
            billType = "AC01";
        }else if("2".equals(query.detail.billType)){
            billType = "AC02";
        }
        xmlWriter.writeElementString("BKTYPE",billType);
//        9 BkType1 票据状态 CHAR(6) T 客户要做撤回的时候，会送待签收的票据状态过来；但由于贴现、追索、清偿等分为两种情况，而且客户做撤回需要将这两种情况的都显示，所以需要规定特殊的状态（其他情况，一律为实际的票据状态）
        xmlWriter.writeElementString("BkType1","");
//        贴现撤回：交易种类0005＋票据状态AAAAAA
//        追索撤回：交易种类0009＋票据状态BBBBBB
//        追索清偿撤回：交易种类0010＋票据状态CCCCCC
//        （逾期）提示付款撤回：交易种类0008＋票据状态DDDDDD
//        10 Bk10Date1 查询起始日期 varChar(8) T YYYYMMDD

        //String operDate = Format.DateTime21Format.format(query.detail.beginDate);
            String begindate = query.detail.operateDate.substring(0,10).replace("-","");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(Format.Date8Format.parse(begindate));
            calendar.add(Calendar.DAY_OF_MONTH,20);
            String enddate = Format.Date8Format.format(calendar.getTime());

        xmlWriter.writeElementString("Bk10Date1",begindate);
//        11 Bk10Date2 查询结束日期 varChar(8) T YYYYMMDD
        xmlWriter.writeElementString("Bk10Date2",enddate);
//        12 Bk8Date3 票据到期日开始  varChar(8) T YYYYMMDD
        xmlWriter.writeElementString("Bk8Date3","");
//        13 Bk8Date4 票据到期日结束  varChar(8) T YYYYMMDD
        xmlWriter.writeElementString("Bk8Date4","");
//        14 Bk60Name1 收款人名称 varChar(120) T 　
        xmlWriter.writeElementString("Bk60Name1","");
//        15 BkAmt1 起始金额 NUMBER(18.2) T 　
        xmlWriter.writeElementString("BkAmt1","");
//        16 BkAmt2 结束金额 NUMBER(18.2) T 　
        xmlWriter.writeElementString("BkAmt2","");
//        18 BkType2 交易类型 varChar(4) F
        String busiType="";
        if("1".equals(query.detail.operType)){
            switch (Integer.valueOf(query.detail.busiType)){
                case 1:busiType = "0000";break;
                case 2:busiType = "0001";break;
                case 3:busiType = "0002";break;
                case 4:busiType = "0004";break;
                case 5:busiType = "0006";break;
                case 6:busiType = "0007";break;
                case 10:busiType = "0005";break;
                case 12:busiType = "0008";break;
                case 13:busiType = "0009";break;
                case 14:busiType = "0010";break;
                default:{
                    throw new Exception("不支持的申请查询类型："+query.detail.operType);
                }
            }
        }else {
            switch (Integer.valueOf(query.detail.busiType)) {
                case 1:busiType = "0003";break;
                case 2:busiType = "0015";break;
                case 3:busiType = "0016";break;
                case 4:busiType = "0017";break;
                case 5:busiType = "0019";break;
                case 6:busiType = "0020";break;
                case 10:busiType = "0018";break;
                case 12:busiType = "0021";break;
                case 13:busiType = "0022";break;
                case 14:busiType = "0023";break;
                default:
                    throw new Exception("不支持的撤销查询类型：" + query.detail.operType);
            }
        }
            xmlWriter.writeElementString("BkType2",busiType);
            //        17 BkListNo1 电子票据号码 varChar(30) T 　
            if(busiType.equals("0000")) {
                xmlWriter.writeElementString("BkListNo1","");
            }else{
                xmlWriter.writeElementString("BkListNo1", query.detail.billNo);
            }
//        19 PAGE 当页号 NUMBER(20) F 默认为1
            xmlWriter.writeElementString("PAGE",page+"");
//        20 PAGE_SIZE 每页显示的记录数 NUMBER(20) F 默认为200条记录数
            xmlWriter.writeElementString("PAGE_SIZE","200");
            xmlWriter.writeEndDocument();
            String[] result = sendToBank(xmlWriter.toXmlString());
            //测试
            if(result[0].length()==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpbkReader!=null){
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg =tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if(StringHelper.hasAnyChar(return_code)&&"000000".equals(return_code)){
                        if(total==0){
                            total = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum"));
                        }
                        while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                            tmpReadNum++;
                            String memo = tmpbkReader.readedXElement.getSingleText("BkDetail3");
                            if(StringHelper.hasAnyChar(memo)&&memo.contains(query.detail.reqSeqID)){
                                detail.respCode ="0";
                                detail.respInfo = tmpbkReader.readedXElement.getSingleText("BkSumry");
                                detail.billNo=tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                detail.billState = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                                detail.transState = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                detail.payInfoCode = detail.transState;
                                flag = true;
                                break;
                            }
                        }
                        if(flag){
                            break;
                        }
                    }else{
                        throw new Exception(return_msg);
                    }
                }else {
                    throw new Exception(tmpsbMsg.toString());
                }
            }else {
                throw new Exception(result[1]);
            }
        }while (tmpReadNum<total);
        if(!flag){
            throw new Exception("处理失败或者银行还未处理，请稍会儿在查");
        }
    }

    /**
     * 对撤销类的操作做状态查询 6WH026）外联平台结果查询
     * @param detail
     * @param query
     * @throws Exception
     */
    private void queryStateCancle(RetAtsB006BillStateQryDetail detail, ReqB006BillStateQry query) throws Exception{
        int page =0;
        int total = 0;
        int tmpReadNum =0;
        //判断只有一笔符合条件的记录
        boolean flag = false;
        do{
            page++;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN =getQryTransID();
            header.TX_CODE = "6WH026";
            writeCommonHeader(xmlWriter,header);
//        Transaction_Body
            xmlWriter.writeStartElement("TX_INFO");
//        8 BKTYPE 票据类型 NUMBER(4) F AC01银承 ;AC02商承
            String billType ="";
            if("1".equals(query.detail.billType)){
                billType ="AC01";
            }else if("2".equals(query.detail.billType)){
                billType = "AC02";
            }
            xmlWriter.writeElementString("BKTYPE",billType);
//        9 BkType1 票据状态 CHAR(6) T 客户要做撤回的时候，会送待签收的票据状态过来；但由于贴现、追索、清偿等分为两种情况，而且客户做撤回需要将这两种情况的都显示，所以需要规定特殊的状态（其他情况，一律为实际的票据状态）
            xmlWriter.writeElementString("BkType1","");
//        贴现撤回：交易种类0005＋票据状态AAAAAA
//        追索撤回：交易种类0009＋票据状态BBBBBB
//        追索清偿撤回：交易种类0010＋票据状态CCCCCC
//        （逾期）提示付款撤回：交易种类0008＋票据状态DDDDDD
//        10 Bk10Date1 查询起始日期 varChar(8) T YYYYMMDD

            //String operDate = Format.DateTime21Format.format(query.detail.beginDate);
            String begindate = query.detail.operateDate.substring(0,10).replace("-","");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(Format.Date8Format.parse(begindate));
            calendar.add(Calendar.DAY_OF_MONTH,20);
            String enddate = Format.Date8Format.format(calendar.getTime());

            xmlWriter.writeElementString("Bk10Date1",begindate);
//        11 Bk10Date2 查询结束日期 varChar(8) T YYYYMMDD
            xmlWriter.writeElementString("Bk10Date2",enddate);
//        12 Bk8Date3 票据到期日开始  varChar(8) T YYYYMMDD
            xmlWriter.writeElementString("Bk8Date3","");
//        13 Bk8Date4 票据到期日结束  varChar(8) T YYYYMMDD
            xmlWriter.writeElementString("Bk8Date4","");
//        14 Bk60Name1 收款人名称 varChar(120) T 　
            xmlWriter.writeElementString("Bk60Name1","");
//        15 BkAmt1 起始金额 NUMBER(18.2) T 　
            xmlWriter.writeElementString("BkAmt1","");
//        16 BkAmt2 结束金额 NUMBER(18.2) T 　
            xmlWriter.writeElementString("BkAmt2","");
//        18 BkType2 交易类型 varChar(4) F
            String busiType="";
            switch (Integer.valueOf(query.detail.busiType)) {
                    case 1:busiType = "0003";break;
                    case 2:busiType = "0015";break;
                    case 3:busiType = "0016";break;
                    case 4:busiType = "0017";break;
                    case 5:busiType = "0019";break;
                    case 6:busiType = "0020";break;
                    case 10:busiType = "0018";break;
                    case 12:busiType = "0021";break;
                    case 13:busiType = "0022";break;
                    case 14:busiType = "0023";break;
                    default:
                        throw new Exception("不支持的撤销查询类型：" + query.detail.operType);
            }
            xmlWriter.writeElementString("BkType2",busiType);
            //        17 BkListNo1 电子票据号码 varChar(30) T 　
            if(busiType.equals("0000")) {
                xmlWriter.writeElementString("BkListNo1","");
            }else{
                xmlWriter.writeElementString("BkListNo1", query.detail.billNo);
            }
//        19 PAGE 当页号 NUMBER(20) F 默认为1
            xmlWriter.writeElementString("PAGE",page+"");
//        20 PAGE_SIZE 每页显示的记录数 NUMBER(20) F 默认为200条记录数
            xmlWriter.writeElementString("PAGE_SIZE","200");
            xmlWriter.writeEndDocument();
            String[] result = sendToBank(xmlWriter.toXmlString());
            //测试
            if(result[0].length()==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpbkReader!=null){
                    String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                    String return_msg =tmpbkReader.getFirstText("/TX/RETURN_MSG");
                    if(StringHelper.hasAnyChar(return_code)&&"000000".equals(return_code)){
                        if(total==0){
                            total = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum"));
                        }
                        while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                            tmpReadNum++;
                            String bkDetail4= tmpbkReader.readedXElement.getSingleText("BkDetail4");
                            if(StringHelper.hasAnyChar(bkDetail4)&&bkDetail4.contains(query.detail.reqSeqID)){
                                detail.respCode ="0";
                                detail.respInfo = tmpbkReader.readedXElement.getSingleText("BkSumry");
                                detail.billNo=tmpbkReader.readedXElement.getSingleText("BkListNo1");
                                detail.billState = tmpbkReader.readedXElement.getSingleText("BkFlag1");
                                detail.transState = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                detail.payInfoCode = detail.transState;
                                flag = true;
                                break;
                            }
                        }
                        if(flag){
                            break;
                        }
                    }else{
                        throw new Exception(return_msg);
                    }
                }else {
                    throw new Exception(tmpsbMsg.toString());
                }
            }else {
                throw new Exception(result[1]);
            }
        }while (tmpReadNum<total);
        if(!flag){
            throw new Exception("没找到符合的记录");
        }
    }

    //追索人查询
    @Override
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception {
        RetAtsB007BillKzsrQry retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo  ="";
        query.readRecord();
        retAts.respCode ="0";
        retAts.billNo = query.detail.billNo;
        retAts.writeRespInfos();
        String[] kzsrTypearr = new String[]{"RT00","RT01"};
        for(String bkType1:kzsrTypearr ){
            try{
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN = getQryTransID();
                header.TX_CODE = "6WH036";
                writeCommonHeader(xmlTextWriter, header);
//                <body>
                xmlTextWriter.writeStartElement("TX_INFO");
//            BkType1 追索类型 varChar(4) F RT00拒付追索 RT01非拒付追索
                xmlTextWriter.writeElementString("BkType1",bkType1);
//            2 Bk60Name1  追索人名称 varChar(120) F 　 　
                xmlTextWriter.writeElementString("Bk60Name1",query.detail.kzsrName);
//            3 BkAcctNo1  追索人账号 varChar(32) F 　
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//            4 BkListNo1  电子票据号码 varChar(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//            5 BkAmt1     票据金额 Decimal(18,2) F 　
                xmlTextWriter.writeElementString("BkAmt1",query.detail.billAmt);
//            6 BkBrchNo1  追索人开户行行号 varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.kzsrBnkCode);
//            7 BkBrchNo2  追索人承接行行号 varChar(12) T
                xmlTextWriter.writeElementString("BkBrchNo2","");
                xmlTextWriter.writeEndDocument();
                String[] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length()==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpbkReader!=null){
                        String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        if(StringHelper.hasAnyChar(return_code)&&"000000".equals(return_code)){
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                                RetAtsB007BillKzsrQryDetail detail = new RetAtsB007BillKzsrQryDetail();
                                detail.receCode = tmpbkReader.readedXElement.getSingleText("BkInstNo");
                                detail.receName = tmpbkReader.readedXElement.getSingleText("Bk60Name3");
                                detail.receAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo3");
                                detail.receBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo3");
                                retAts.writeDetail(detail);
                            }
                        }else{
                            respCode ="-1";
                            respInfo = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                        }
                    }else {
                        respCode ="-1";
                        respInfo = tmpsbMsg.toString();
                    }
                }else {
                    respCode = "-1";
                    respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                respCode ="-1";
                respInfo = ex.getMessage();
            }
        }
//        if("-1".equals(respCode)){
//            retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
//            retAts.writeStart();
//            retAts.respCode ="-1";
//            retAts.respInfo =respInfo;
//            retAts.billNo = query.billNo;
//            retAts.writeRespInfos();
//        }
        retAts.writeEnd();
        return retAts;
    }
//票据操作信息查询
    @Override
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception {
        RetAtsB008HistoricalBillQry retAts = new RetAtsB008HistoricalBillQry(query.reqHeader);
        retAts.writeBefore();
        String respCode ="";
        String respInfo ="";
        try{
            while (query.readRecord()) {
                int page = 1;
                int curNum = 0;
                int totalNum = -1;
                while (curNum <totalNum || totalNum == -1) {
                    XmlTextWriter xmlTextWriter = new XmlTextWriter();
                    CCB02Header header = new CCB02Header();
                    header.REQUEST_SN = getQryTransID();
                    header.TX_CODE = "6WH033";
                    writeCommonHeader(xmlTextWriter, header);
                    xmlTextWriter.writeStartElement("TX_INFO");
//                page     页次            NUMBER(6) T 　
                    xmlTextWriter.writeElementString("page", page+"");
//                2 BKTYPE   票据种类           varChar(4) F 　 　
                    xmlTextWriter.writeElementString("BKTYPE", "1".equals(query.detail.billType)?"AC01":"AC02");
//                3 BkType1  交易种类           varChar(4) F 0001：背书转出 0002：背书转入 0003：质押质出 0004：质押质入 0005：贴现贴出 0006：贴现赎回 0007：质押解除
                    String bkType;
                    switch (Integer.valueOf(query.detail.queryType)) {
                        case 1: {
                            if ("2".equals(query.detail.busiType)) {
                                bkType = "0001";
                            } else if ("3".equals(query.detail.busiType)) {
                                bkType = "0005";
                            } else if ("10".equals(query.detail.busiType)) {
                                bkType = "0003";
                            } else {
                                throw new Exception("不要不支持该业务类型的查询：" + query.detail.busiType);
                            }
                        }
                        break;
                        case 2: {
                            if ("2".equals(query.detail.busiType)) {
                                bkType = "0002";
                            } else if ("3".equals(query.detail.busiType)) {
                                bkType = "0006";
                            } else if ("10".equals(query.detail.busiType)) {
                                bkType = "0004";
                            } else {
                                throw new Exception("不要不支持该业务类型的查询：" + query.detail.busiType);
                            }
                        }
                        break;
                        default:
                            throw new Exception("未知的查询类型：" + query.detail.queryType);
                    }

                    xmlTextWriter.writeElementString("BkType1",bkType );
//                4 BkFlag1  票据状态           varChar(6) T 　 　
                    xmlTextWriter.writeElementString("BkFlag1", "");
//                5 Bk8Date1 查询起始日期       Char(8) T YYYYMMDD
                    xmlTextWriter.writeElementString("Bk8Date1", query.detail.dueDateDown);
//                6 Bk8Date2 查询结束日期       Char(8) T YYYYMMDD
                    xmlTextWriter.writeElementString("Bk8Date2", query.detail.dueDateUp);
//                7 BkAmt1   票据金额1          Decimal(18,2) T 　
                    xmlTextWriter.writeElementString("BkAmt1", "");
//                8 BkAmt2   票据金额2          Decimal(18,2) T 　
                    xmlTextWriter.writeElementString("BkAmt2", "");
//                9 PAGE_SIZE            每页最大记录数          Number(20) T 默认为200条记录数
                    xmlTextWriter.writeElementString("PAGE_SIZE", "40");
                    String [] result = sendToBank(xmlTextWriter.toXmlString());
                    if(result[0].length()==0){
                        StringBuilder tmpsbMsg = new StringBuilder();
                        XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                        if(tmpbkReader!=null){
                            String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                            if(StringHelper.hasAnyChar(return_code)&&"0".equals(return_code)){
                                if(StringHelper.isNullOrEmpty(retAts.respCode)) {
                                    retAts.respCode = "0";
                                    retAts.respInfo = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                                    totalNum = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum"));
                                }
                                int returnNum = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1"));
                                while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                                    RetAtsB008HistoricalBillQryDetail detail = new RetAtsB008HistoricalBillQryDetail();
//                                    <BkListNo1>电子票据号码</BkListNo1>
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("");
//                                    <Bk60Name1>持票人全称</Bk60Name1>
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("");
//                                    <BkAcctNo1>持票人账号</BkAcctNo1>
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("");
//                                    <BkType1>票据种类</BkType1>
//                                    <Bk60Name2>保证人名称</Bk60Name2>
//                                    <BkBrchNo2>承兑银行</BkBrchNo2>
//                                    <Bk60Name5>承兑人名称</Bk60Name5>
//                                    <BkAcctNo2>承兑人账号</BkAcctNo2>
//                                    <BkAmt1>出票金额</BkAmt1>
//                                    <Bk8Date1>出票日期</Bk8Date1>
//                                    <Bk8Date2>汇票到期日</Bk8Date2>
//                                    <BkFlag1>票据状态</BkFlag1>
//                                    <Bk60Name3>背书人名称</Bk60Name3>
//                                    <Bk60Name4>被背书人名称</Bk60Name4>
//                                    </DETAILINFO>
//                                    </DETAILLIST>
//                                    </TX_INFO>
                                }
                            }else{
                                respCode ="-1";
                                respInfo = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                            }
                        }else {
                            respCode ="-1";
                            respInfo = tmpsbMsg.toString();
                        }
                    }else {
                        respCode = "-1";
                        respInfo = result[1];
                    }

                }
            }
        }catch (Exception ex){

        }
        return null;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        RetAtsB009DrawBillQry retAts = new RetAtsB009DrawBillQry(query.reqHeader);
        retAts.writeStart();
        query.readRecord();
        try{
        List<RetAtsB009DrawBillQryProtocolDetail> protocols = getProtocolDetails();
        for (RetAtsB009DrawBillQryProtocolDetail protocolDetail : protocols) {
            queryBillBy6WH024(query.detail.actNum,retAts,protocolDetail);
        }
            retAts.respCode ="0";
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo =ex.getMessage();
        }
        retAts.writeRespCode( retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        RetAtsB010ProtocolStateQry retAts = new RetAtsB010ProtocolStateQry(reqHeader);
        retAts.writeStart();
        query.readRecord();
        int page =0;
        int total = 0;
        int tmpReadNum =0;
        boolean flag =false;
        try {
            do {
                page++;
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN = getQryTransID();
                header.TX_CODE = "6WH026";
                writeCommonHeader(xmlWriter, header);
//        Transaction_Body
                xmlWriter.writeStartElement("TX_INFO");
//        8 BKTYPE 票据类型 NUMBER(4) F AC01银承 ;AC02商承
                xmlWriter.writeElementString("BKTYPE", "AC01");
//        9 BkType1 票据状态 CHAR(6) T 客户要做撤回的时候，会送待签收的票据状态过来；但由于贴现、追索、清偿等分为两种情况，而且客户做撤回需要将这两种情况的都显示，所以需要规定特殊的状态（其他情况，一律为实际的票据状态）
                xmlWriter.writeElementString("BkType1", "");
//        贴现撤回：交易种类0005＋票据状态AAAAAA
//        追索撤回：交易种类0009＋票据状态BBBBBB
//        追索清偿撤回：交易种类0010＋票据状态CCCCCC
//        （逾期）提示付款撤回：交易种类0008＋票据状态DDDDDD
//        10 Bk10Date1 查询起始日期 varChar(8) T YYYYMMDD

                //String operDate = Format.DateTime21Format.format(query.detail.beginDate);
                String begindate = query.detail.operateDate.substring(0, 10).replace("-", "");

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(Format.Date8Format.parse(begindate));
                calendar.add(Calendar.DAY_OF_MONTH, 3);
                String enddate = Format.Date8Format.format(calendar.getTime());

                xmlWriter.writeElementString("Bk10Date1", begindate);
//        11 Bk10Date2 查询结束日期 varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk10Date2", enddate);
//        12 Bk8Date3 票据到期日开始  varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk8Date3", "");
//        13 Bk8Date4 票据到期日结束  varChar(8) T YYYYMMDD
                xmlWriter.writeElementString("Bk8Date4", "");
//        14 Bk60Name1 收款人名称 varChar(120) T 　
                xmlWriter.writeElementString("Bk60Name1", "");
//        15 BkAmt1 起始金额 NUMBER(18.2) T 　
                xmlWriter.writeElementString("BkAmt1", "");
//        16 BkAmt2 结束金额 NUMBER(18.2) T 　
                xmlWriter.writeElementString("BkAmt2", "");
//        18 BkType2 交易类型 varChar(4) F

                xmlWriter.writeElementString("BkType2", "0000");
                //        17 BkListNo1 电子票据号码 varChar(30) T 　
                xmlWriter.writeElementString("BkListNo1", "");

//        19 PAGE 当页号 NUMBER(20) F 默认为1
                xmlWriter.writeElementString("PAGE", page + "");
//        20 PAGE_SIZE 每页显示的记录数 NUMBER(20) F 默认为200条记录数
                xmlWriter.writeElementString("PAGE_SIZE", "200");
                xmlWriter.writeEndDocument();
                String[] result = sendToBank(xmlWriter.toXmlString());

                //测试
                if (result[0].length() == 0) {
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                    if (tmpbkReader != null) {
                        String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                        String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                        if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {
                            if (total == 0) {
                                total = Integer.valueOf(tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum"));
                            }
                            while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                tmpReadNum++;
                                String protocol = tmpbkReader.readedXElement.getSingleText("BkListNo2");
                                if (StringHelper.hasAnyChar(protocol) && protocol.equals(query.detail.protocolNo)) {
                                    if (!flag) {
                                        retAts.protocolNo = protocol;
                                        retAts.respCode = "0";
                                        retAts.transState = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                        retAts.payInfoCode = retAts.transState;
                                        retAts.reqSeqID = query.detail.reqSeqID;
                                        flag = true;
                                    }
                                    RetAtsB010ProtocolStateQryDetail detail = new RetAtsB010ProtocolStateQryDetail();
//                                9 BkListNo1 电子票据号码 varChar(30) F 　
                                    detail.billNo = tmpbkReader.readedXElement.getSingleText("BkListNo1");
//                                10 Bk8Date1 出票日期 varChar(8) F 　
                                    detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date1"));
//                                11 Bk8Date2 汇票到期日 varChar(8) F 　
                                    detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("Bk8Date2"));
//                                12 BkAmt1 出票金额 NUMBER(18.2) F 　
                                    detail.billAmt = tmpbkReader.readedXElement.getSingleText("BkAmt1");
//                                13 BkListNo5 票据编号 varChar(40) F
                                    detail.billNum = tmpbkReader.readedXElement.getSingleText("BkListNo5");
//                                14 BkAcctNo1 出票账号 varChar(32) F 　
                                    detail.drawAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo1");
//                                15 Bk60Name1 出票人名称 varChar(120) F 　
                                    detail.drawName = tmpbkReader.readedXElement.getSingleText("Bk60Name1");
//                                16 BkBrchNo1 出票人开户行 varChar(12) F 　
                                    detail.drawBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo1");
//                                17 BkAcctNo2 收款人账号 varChar(32) F 　
                                    detail.payeeAct = tmpbkReader.readedXElement.getSingleText("BkAcctNo2");
//                                18 Bk60Name2 收款人名称 varChar(120) F 　
                                    detail.payeeName = tmpbkReader.readedXElement.getSingleText("Bk60Name2");
//                                19 BkBrchNo2 收款人开户行号 varChar(12) F 　
                                    detail.payeeBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo2");
//                                20 BkAmt2 追索金额 NUMBER(18,2) F 交易类型为0009（追索）时候，返回追索金额
                                    detail.accpBnkCode = tmpbkReader.readedXElement.getSingleText("BkBrchNo4");
//                                22 Bk60Name4 承兑人名称 varChar(120) F 　
                                    detail.accpName = tmpbkReader.readedXElement.getSingleText("Bk60Name4");
//                                25 BkFlag1 票据状态 varChar(6) F 　
                                    detail.billState = tmpbkReader.readedXElement.getSingleText("BkFlag1");
//                                BkFlag2 交易结果 CHAR(2) F
                                    detail.result = tmpbkReader.readedXElement.getSingleText("BkFlag2");
                                    retAts.writeDetail(detail);
                                }
                            }
                        } else {
                            throw new Exception(return_msg);
                        }
                    } else {
                        throw new Exception(tmpsbMsg.toString());
                    }
                } else {
                    throw new Exception(result[1]);
                }
            } while (tmpReadNum < total);
            if (!flag) {
                throw new Exception("处理失败或者银行还未处理，请稍会儿在查");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeBefore();
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }
}
